﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.WindowsAzure.ServiceRuntime
Imports System.Globalization

Namespace Infrastructure

    Public NotInheritable Class ConfigReader
        Private Sub New()
        End Sub

        <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification:="This method gets a configuration setting value.")>
        Public Shared Function GetConfigValue(ByVal key As String, Optional ByVal shouldValidate As Boolean = True) As String
            Dim value = String.Empty
            If RoleEnvironment.IsAvailable Then
                value = RoleEnvironment.GetConfigurationSettingValue(key)
            Else
                value = ConfigurationManager.AppSettings(key)
            End If

            If shouldValidate Then
                Validate(key, value)
            End If

            Return value
        End Function

        Public Shared Function GetWebConfigValue(ByVal key As String) As String
            Dim value = ConfigurationManager.AppSettings(key)

            Validate(key, value)

            Return value
        End Function

        Private Shared Sub Validate(ByVal key As String, ByVal value As String)
            If String.IsNullOrWhiteSpace(value) Then
                Throw New ArgumentException(String.Format(CultureInfo.InvariantCulture, "The '{0}' setting is not available.", key), "key")
            End If
        End Sub
    End Class
End Namespace